<?php

include( "./admin/include/func.php");
include( "Pager/Pager.php");

$db = new db();
if ( ! $db->connect()) exit( mysql_error());

$db->sql = "SELECT COUNT(id)
            FROM hirdetes 
            ORDER BY azonosito";		
$db->query();

$rpp = 500; //row per page
$tct = mysql_fetch_row($db->rs);
$tct = $tct[0];	//total count
$pager_options = array('mode' => 'Sliding', 'perPage' => $rpp, 'delta' => 2, 'totalItems' => $tct,);
$pager = @Pager::factory($pager_options);
list($from, $to) = $pager->getOffsetByPageId();
	
$db->sql = "SELECT azonosito,targy,rovat_nev 
            FROM hirdetes
            ORDER BY azonosito 
            LIMIT " . ($from - 1) . ", $rpp";					
$db->query();

$hirds = array();
while ($h = mysql_fetch_assoc( $db->rs)) $hirds[] = $h; 

foreach ($hirds as $hird) {
		
	$rovatok = explode( ' - ', $hird['rovat_nev']);

	$forovat = $rovatok[0];
	
	if ($forovat == 'Járm?') $forovat = 'Jármű';

	$alrovat = '';
	$sep = '';
	for( $i=1;$i<count( $rovatok);$i++) {
		$alrovat .= $sep . $rovatok[$i];	
		$sep = ' - ';
	}
	
	if ($forovat != '') {
		
		$db->sql = "SELECT id,nev FROM rovat WHERE nev='$forovat' AND parent=0 LIMIT 1";
		$db->query();

		$forov = mysql_fetch_assoc( $db->rs);
		
		$db->sql = "SELECT id,nev FROM rovat WHERE nev='$alrovat' AND parent=".$forov['id']." LIMIT 1";
		$db->query();

		$alrov = mysql_fetch_assoc( $db->rs);
	
	} else {
		
		$forov = array ("id"=>1, "nev"=>"Általános kereskedelem");
	}
	
	if ( count( $forov) < 2) {
		print 'BIG HIBA!!!!!!!!!!!';
		debug( $hird, 'hird');
		debug( $rovatok, 'rovatok');
		debug( $forovat, 'forovat');
		debug( $alrovat, 'alrovat');
		debug( $forov, 'forov');
		debug( $alrov, 'alrov');
		print "<hr><hr><hr><br><br>";
		//break;
	} 
	
	$rovat_id = $forov['id'];
	if ( count( $alrov) > 1) $rovat_id = $alrov['id'];

	//debug( $rovat_id, 'rovat_id');
	//print "<hr><hr><hr><br><br>";
			
	$db->sql = "UPDATE hirdetes SET rovat=$rovat_id WHERE azonosito='".$hird['azonosito']."'";				
	$db->query();
	
	//$db->sql = "SELECT azonosito,targy,rovat,feladas FROM hirdetes WHERE azonosito='".$hird['azonosito']."'"; 
	//$db->query();
	//$mh = mysql_fetch_assoc( $db->rs);
	//debug( $mh, 'mh');
}

?>

<br />

<div id="pager"><?php echo $pager->links; ?></div>


